{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chipping directory 2012\n",
      "/workspace/storage/data/whucd/Building change detection dataset_add/1. The two-period image data/2012/whole_image/train/image /workspace/storage/data/whucd-chipped/2012/train\n",
      "Obtained 4838 image slices!\n",
      "/workspace/storage/data/whucd/Building change detection dataset_add/1. The two-period image data/2012/whole_image/test/image /workspace/storage/data/whucd-chipped/2012/test\n",
      "Obtained 2596 image slices!\n",
      "Chipping directory 2016\n",
      "/workspace/storage/data/whucd/Building change detection dataset_add/1. The two-period image data/2016/whole_image/train/image /workspace/storage/data/whucd-chipped/2016/train\n",
      "Obtained 4838 image slices!\n",
      "/workspace/storage/data/whucd/Building change detection dataset_add/1. The two-period image data/2016/whole_image/test/image /workspace/storage/data/whucd-chipped/2016/test\n",
      "Obtained 2596 image slices!\n",
      "Chipping directory change_label\n",
      "/workspace/storage/data/whucd/Building change detection dataset_add/1. The two-period image data/change_label/train /workspace/storage/data/whucd-chipped/change_label/train\n",
      "Obtained 4838 image slices!\n",
      "/workspace/storage/data/whucd/Building change detection dataset_add/1. The two-period image data/change_label/test /workspace/storage/data/whucd-chipped/change_label/test\n",
      "Obtained 2596 image slices!\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from image_bbox_slicer import Slicer\n",
    "from PIL import Image\n",
    "import image_bbox_slicer.helpers\n",
    "\n",
    "image_bbox_slicer.helpers.IMG_FORMAT_LIST.append(\"tif\")\n",
    "Image.MAX_IMAGE_PIXELS = None\n",
    "\n",
    "files = {\n",
    "    \"2012\": {\n",
    "        \"train\": os.path.join(\n",
    "            \"2012\", \"whole_image\", \"train\", \"image\", \"2012_train.tif\"\n",
    "        ),\n",
    "        \"test\": os.path.join(\"2012\", \"whole_image\", \"test\", \"image\", \"2012_test.tif\"),\n",
    "    },\n",
    "    \"2016\": {\n",
    "        \"train\": os.path.join(\n",
    "            \"2016\", \"whole_image\", \"train\", \"image\", \"2016_train.tif\"\n",
    "        ),\n",
    "        \"test\": os.path.join(\"2016\", \"whole_image\", \"test\", \"image\", \"2016_test.tif\"),\n",
    "    },\n",
    "    \"change_label\": {\n",
    "        \"train\": os.path.join(\"change_label\", \"train\", \"change_label.tif\"),\n",
    "        \"test\": os.path.join(\"change_label\", \"test\", \"change_label.tif\"),\n",
    "    },\n",
    "}\n",
    "patch_size = (256, 256)\n",
    "root = \"/workspace/storage/data/whucd/Building change detection dataset_add/1. The two-period image data/\"\n",
    "output = \"/workspace/storage/data/whucd-chipped/\"\n",
    "\n",
    "for directory in files:\n",
    "    print(f\"Chipping directory {directory}\")\n",
    "    for split in files[directory]:\n",
    "        src = os.path.join(root, os.path.dirname(files[directory][split]))\n",
    "        dst = os.path.join(output, directory, split)\n",
    "        print(src, dst)\n",
    "        slicer = Slicer()\n",
    "        slicer.config_image_dirs(img_src=src, img_dst=dst)\n",
    "        slicer.slice_images_by_size(tile_size=patch_size, tile_overlap=0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torchenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
